/*
 * Copyright 2009 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.appengine.demos.oauth;

import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPResponse;

/**
 * Metadata returned from an oauthfetch.
 */
public class OAuthFetchResponse {
  private final HTTPResponse response;
  private final String oauthState;
  private final String oauthApprovalUrl;
  private final String oauthError;
  private final String oauthErrorText;
  
  public OAuthFetchResponse(HTTPResponse response) {
    this.response = response;
    String oauthState = null;
    String oauthApprovalUrl = null;
    String oauthError = null;
    String oauthErrorText = null;
    for (HTTPHeader header : response.getHeaders()) {
      String name = header.getName();
      if ("x_oauth_state".equals(name)) {
        oauthState = header.getValue();
      } else if ("x_oauth_approval_url".equals(name)) {
        oauthApprovalUrl = header.getValue();
      } else if ("x_oauth_error".equals(name)) {
        oauthError = header.getValue();
      } else if ("x_oauth_error_text".equals(name)) {
        oauthErrorText = header.getValue();
      }
    }
    this.oauthState = oauthState;
    this.oauthApprovalUrl = oauthApprovalUrl;
    this.oauthError = oauthError;
    this.oauthErrorText = oauthErrorText;
  }

  public HTTPResponse getResponse() {
    return response;
  }

  public String getOAuthApprovalUrl() {
    return oauthApprovalUrl;
  }

  public String getOAuthState() {
    return oauthState;
  }

  public String getOAuthError() {
    return oauthError;
  }

  public String getOAuthErrorText() {
    return oauthErrorText;
  }
}
